$(function () {
    var animateEventEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
    var previewPhone = $('#preview-phone .preview-phone');
    var previewShareCard = $('#preview-phone .preview-share-media');
    var originalSrc = previewPhone.attr('originalSrc');
    var previewContainer = $('#preview-container');
    var previewTrigger = $('#preview-trigger');
    var previewShow = previewTrigger.find('.preview-trigger-show');
    var previewReturn = previewTrigger.find('.preview-trigger-return');
    previewShow.on('click', function (e) {
        if (!previewContainer.hasClass('hidden')) {
            return false;
        }
        previewContainer.removeClass('hidden');
        $('body').addClass('overflow-hidden');
        previewContainer.addClass('scaleInRightBottom animated').one(animateEventEnd, function () {
            previewContainer.removeClass('scaleInRightBottom animated');
            previewContainer.unbind(animateEventEnd);
        });
        previewShow.addClass('hidden');
        previewReturn.removeClass('hidden');
        previewReturn.addClass('flip animated').one(animateEventEnd, function () {
            previewReturn.removeClass('flip animated');
            previewReturn.unbind(animateEventEnd);
        });
        previewPhone.attr('src', originalSrc);
        previewShareCard.attr('src', previewShareCard.attr('originalSrc'));
    });
    previewReturn.on('click', function () {
        if (previewContainer.hasClass('hidden')) {
            return false;
        }
        $('body').removeClass('overflow-hidden');
        previewContainer.addClass('scaleOutRightBottom animated').one(animateEventEnd, function () {
            previewContainer.removeClass('scaleOutRightBottom animated');
            previewContainer.addClass('hidden');
            previewContainer.unbind(animateEventEnd);
        });
        previewReturn.addClass('hidden');
        previewShow.removeClass('hidden');
        previewShow.addClass('flip animated').one(animateEventEnd, function () {
            previewShow.removeClass('flip animated');
            previewShow.unbind(animateEventEnd);
        });
    });

    var canvasContainer = $('#previewPhoneQRCode');
    canvasContainer.html('');
    canvasContainer.qrcode({
        width: 140,
        height: 140,
        foreground: '#05686d',
        text: originalSrc
    });
    canvasContainer.append('<img src="'+canvasContainer.data('logo')+'"/>');

    var meizhuHome = $('#preview-phone').attr('original');
    var meizhuHotelName = $('#preview-phone').data('name');
    var meizhuHotelDesc = $('#preview-phone').data('desc');
    var meizhuLogo = $('#preview-phone').attr('data-logo');
    var meizhuShareMedia = meizhuHome+'/book/'+window.currentHotel;
    $('#preview-phone .preview-share-action-wechat').on('shown.bs.tooltip', function(){
        var wechatCanvasContainer = $('#preview-container .tooltipCanvasContainer');
        wechatCanvasContainer.find('canvas').remove();
        wechatCanvasContainer.qrcode({
            width: 108,
            height: 108,
            foreground: '#3eb135',
            text: meizhuShareMedia
        });
    });

    $('#preview-phone .preview-share-action-weibo').on('click', function(){
        var params = {
            url: meizhuShareMedia,
            searchPic: 'false',
            title: meizhuHotelName + '\n'+ meizhuHotelDesc,
            appkey: 2457203653,
            ralateUid: 5943126717,
        };
        var paramStrs = [];
        for(index in params){
            paramStrs.push(index +'='+encodeURIComponent(params[index]));
        }
        window.open('http://service.weibo.com/share/share.php?'+ paramStrs.join('&'));
    });

    $('#preview-phone .preview-share-action-qqzone').on('click', function(){
        var params = {
            url: meizhuShareMedia,
            site: meizhuHome,
            title: meizhuHotelName,
            summary: meizhuHotelDesc,
            pics:meizhuLogo
        };
        var paramStrs = [];
        for(index in params){
            paramStrs.push(index +'='+encodeURIComponent(params[index]));
        }
        window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?'+ paramStrs.join('&'));
    });

    var phoneWindow;
    var newWindowLoad = function(){
        html2canvas(phoneWindow.document.body).then(function(canvas) {
            canvas.toBlob(function(blob) {
                saveAs(blob, "广告.png");
            });
        });
    };

    $('#preview-media-download').on('click', function(e){
        e.preventDefault();
        phoneWindow = window.open(meizhuShareMedia, meizhuHotelName, 'height=667,width=375,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
        phoneWindow.onload = newWindowLoad;
    });
});
